/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.shop;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCollageCoupon;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopCoupon;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopCollageCouponService;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopCouponService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拼团优惠券Controller
 * @author 李欣荣
 * @version 2018-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/shop/hkShopCollageCoupon")
public class HkShopCollageCouponController extends BaseController {

	@Autowired
	private HkShopCollageCouponService hkShopCollageCouponService;

	@Autowired
	private HkShopCouponService hkShopCouponService;


	@ModelAttribute
	public HkShopCollageCoupon get(@RequestParam(required=false) String id) {
		HkShopCollageCoupon entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hkShopCollageCouponService.get(id);
		}
		if (entity == null){
			entity = new HkShopCollageCoupon();
		}
		return entity;
	}
	
	@RequiresPermissions("shop:shop:hkShopCollageCoupon:view")
	@RequestMapping(value = {"list", ""})
	public String list(HkShopCollageCoupon hkShopCollageCoupon, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HkShopCollageCoupon> page = hkShopCollageCouponService.findPage(new Page<HkShopCollageCoupon>(request, response), hkShopCollageCoupon); 
		model.addAttribute("page", page);
		return "hk/shop/shop/hkShopCollageCouponList";
	}

	@RequiresPermissions("shop:shop:hkShopCollageCoupon:view")
	@RequestMapping(value = "form")
	public String form(HkShopCollageCoupon hkShopCollageCoupon, Model model) {
		model.addAttribute("hkShopCollageCoupon", hkShopCollageCoupon);
		return "hk/shop/shop/hkShopCollageCouponForm";
	}

	@RequiresPermissions("shop:shop:hkShopCollageCoupon:edit")
	@RequestMapping(value = "save")
	public String save(HkShopCollageCoupon hkShopCollageCoupon, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hkShopCollageCoupon)){
			return form(hkShopCollageCoupon, model);
		}

		/**
		 * 根据优惠券ID获取商品ID、折扣
		 * 优惠券生效时间、优惠券过期时间(暂时不做控制)
		 */
		String shopPouponId = hkShopCollageCoupon.getCoupon().getId();
		if (StringUtils.isBlank(shopPouponId)){
			addMessage(redirectAttributes, "保存拼团优惠券失败");
			return "redirect:"+Global.getAdminPath()+"/shop/shop/hkShopCollageCoupon/?repage";
		}else {
			HkShopCoupon hkShopCoupon =hkShopCouponService.get(shopPouponId);
			hkShopCollageCoupon.setProductId(hkShopCoupon.getProduct().getId());	//商品ID
			hkShopCollageCoupon.setDiscount(hkShopCoupon.getDiscount());  //折扣
			hkShopCollageCouponService.save(hkShopCollageCoupon);
			addMessage(redirectAttributes, "保存拼团优惠券成功");
			return "redirect:"+Global.getAdminPath()+"/shop/shop/hkShopCollageCoupon/?repage";
		}

	}
	
	@RequiresPermissions("shop:shop:hkShopCollageCoupon:edit")
	@RequestMapping(value = "delete")
	public String delete(HkShopCollageCoupon hkShopCollageCoupon, RedirectAttributes redirectAttributes) {
		hkShopCollageCouponService.delete(hkShopCollageCoupon);
		addMessage(redirectAttributes, "删除拼团优惠券成功");
		return "redirect:"+Global.getAdminPath()+"/shop/shop/hkShopCollageCoupon/?repage";
	}

}